//
//  TitleImageBottomView.m
//  JJHW_coreData
//
//  Created by lizhenqiang on 16/4/10.
//  Copyright © 2016年 HFKJ_LZQ. All rights reserved.
//

#import "TitleImageBottomView.h"
#import "SplashObj.h"
#import "SplashChild.h"
#import "JJotherViewController.h"
@implementation TitleImageBottomView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
-(id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.myWidth = frame.size.width;
        self.myHeight = frame.size.height;
        self.carouselView.frame = CGRectMake(0, 0, self.myWidth, self.myHeight);
    }
    return self;
}


-(void)setViewData:(NSMutableArray *)array
{
    self.dataArray = array;
    [self.carouselView reloadData];
}
#pragma mark - iCarousel methods

- (NSInteger)numberOfItemsInCarousel:(__unused iCarousel *)carousel
{
    return self.dataArray.count;
}

- (UIView *)carousel:(__unused iCarousel *)carousel viewForItemAtIndex:(NSInteger)index reusingView:(UIView *)view
{
    /*
     - //iOS8需要设置水平 和垂直对齐的两个属性
     - btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
     - btn.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
     -   [btn setTitleEdgeInsets:UIEdgeInsetsMake(60, -55, 0, 0)];//上左下右四个属性
     */
    SplashObj *obj = self.dataArray[index];
    UIButton *btn = nil;
    CGFloat height = 49;
    CGFloat width = DeviceWidth/(self.dataArray.count);
    if (view == nil) {
        view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, width, height)];
        btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, width, height)];
        btn.tag = 100;
        
        [btn setBackgroundImage:[UIColor imageFromColor:CGRectMake(0, 0, width, height) andColor:TabBarBjNormalColor] forState:UIControlStateNormal];
        [btn setBackgroundImage:[UIColor imageFromColor:CGRectMake(0, 0, width, height) andColor:TabBarBjSelectColor] forState:UIControlStateSelected];
        btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        btn.contentVerticalAlignment = UIControlContentVerticalAlignmentTop;//
        [btn setTitle:obj.name forState:UIControlStateNormal];
        [btn setTitle:obj.name forState:UIControlStateSelected];
        [btn sd_setImageWithURL:[NSURL URLWithString:obj.icon] forState:UIControlStateNormal placeholderImage:obj.placeImage];
        [btn sd_setImageWithURL:[NSURL URLWithString:obj.icon] forState:UIControlStateSelected placeholderImage:obj.placeImage];
        [btn setImageEdgeInsets:UIEdgeInsetsMake(4, -(width-12)/2, 0, 0)];
        [btn setTitleEdgeInsets:UIEdgeInsetsMake(0, -((width-45)/2-12), 4, 0)];
        [view addSubview:btn];
    }
    else
    {
        btn = (UIButton *)[view viewWithTag:100];
        
    }
    [btn setBackgroundImage:[UIColor imageFromColor:CGRectMake(0, 0, width, height) andColor:TabBarBjNormalColor] forState:UIControlStateNormal];
    [btn setBackgroundImage:[UIColor imageFromColor:CGRectMake(0, 0, width, height) andColor:TabBarBjSelectColor] forState:UIControlStateSelected];
    btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    btn.contentVerticalAlignment = UIControlContentVerticalAlignmentTop;//
    [btn setTitle:obj.name forState:UIControlStateNormal];
    [btn setTitle:obj.name forState:UIControlStateSelected];
    [btn sd_setImageWithURL:[NSURL URLWithString:obj.icon] forState:UIControlStateNormal placeholderImage:obj.placeImage];
    [btn sd_setImageWithURL:[NSURL URLWithString:obj.icon] forState:UIControlStateSelected placeholderImage:obj.placeImage];
    [btn setImageEdgeInsets:UIEdgeInsetsMake(4, -(width-12)/2, 0, 0)];
    [btn setTitleEdgeInsets:UIEdgeInsetsMake(0, -((width-45)/2-12), 4, 0)];
    return view;
    
}
- (void)carousel:(iCarousel *)carousel didSelectItemAtIndex:(NSInteger)index
{
    //self.VC push
    JJotherViewController *jjother = [[JJotherViewController alloc] init];
    [self.VC.navigationController pushViewController:jjother animated:YES];
    
}
@end
